package com.lvmama.pangolin.timer.processor;



import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.lvmama.pangolin.busi.config.ConfigManager;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.service.ConfigService;
import com.lvmama.pangolin.timer.CommonJob;
import com.lvmama.pangolin.util.SpringContextUtil;


public class SQLActor extends CommonJob {

	@Autowired
	private ConfigService configService;
	
	private Logger logger = Logger.getLogger(SQLActor.class);
	@Override
	public void runJob(JobExecutionContext context, String parameter)
	{
		try
		{
			
			logger.info("执行SQL开始...");
			if(configService == null)
			{
				configService = (ConfigService) SpringContextUtil.getBean(ConfigService.class);
			}
			Map<String,Object> param = new HashMap<String,Object>();
			ConfigManager config = ConfigManager.getInstance();
			String[] paramNameArr = config.getParamNamesByConfigName(parameter);
			if (paramNameArr != null && paramNameArr.length > 0)
			{
				String value = null;
				for (String string : paramNameArr)
				{
					value = config.getParamValue(parameter, string);
					if(StringTools.isValid(value))
					{
						param.put(string, value);
					}
				}
			}
			configService.executeSql(param);
			logger.info("执行SQL结束...SQL : " + param.get("sql"));
			
		}catch (Exception e)
		{
			logger.error("执行SQL失败!",e);
		}
		
		
		
	}

}
